package com.yhn.init.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.init.common.DeleteRequest;
import com.yhn.init.common.ErrorCode;
import com.yhn.init.exception.BusinessException;
import com.yhn.init.exception.ThrowUtils;
import com.yhn.init.mapper.InventoryMapper;
import com.yhn.init.model.dto.inventory.InventoryAddRequest;
import com.yhn.init.model.dto.inventory.InventoryQueryRequest;
import com.yhn.init.model.dto.inventory.InventoryUpdateRequest;
import com.yhn.init.model.entity.Inventory;
import com.yhn.init.model.vo.InventoryVO;
import com.yhn.init.service.InventoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yhnyhn123
 */
@Slf4j
@Service("inventoryService")
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {

    @Override
    public Page<InventoryVO> queryPage(InventoryQueryRequest inventoryQueryRequest) {
        if (inventoryQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = inventoryQueryRequest.getCurrent();
        long size = inventoryQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Inventory> inventoryPage = this.page(new Page<>(current, size),
                this.getQueryWrapper(inventoryQueryRequest));
        Page<InventoryVO> inventoryVOPage = new Page<>(current, size, inventoryPage.getTotal());
        List<InventoryVO> inventoryVO = this.getInventoryVO(inventoryPage.getRecords());
        inventoryVOPage.setRecords(inventoryVO);
        return inventoryVOPage;
    }


    @Override
    public QueryWrapper<Inventory> getQueryWrapper(InventoryQueryRequest inventoryQueryRequest) {
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        // 如果请求对象为空，直接返回一个空的查询包装器
        if (inventoryQueryRequest == null) {
            return queryWrapper;
        }
        // 从请求对象中获取各项查询条件
        queryWrapper.eq(inventoryQueryRequest.getItemName() != null, "item_name", inventoryQueryRequest.getItemName());
        queryWrapper.eq(inventoryQueryRequest.getType() != null, "type", inventoryQueryRequest.getType());
        return queryWrapper;
    }

    /**
     * 获取脱敏对象集合
     *
     * @param inventoryList 查询请求
     * @return 脱敏对象集合
     */
    @Override
    public List<InventoryVO> getInventoryVO(List<Inventory> inventoryList) {
        if (CollUtil.isEmpty(inventoryList)) {
            return new ArrayList<>();
        }
        return inventoryList.stream().map(this::getInventoryVO).collect(Collectors.toList());
    }


    /**
     * 将对象转化为脱敏对象
     *
     * @param inventory 原对象
     * @return 脱敏对象
     */
    @Override
    public InventoryVO getInventoryVO(Inventory inventory) {
        if (inventory == null) {
            return null;
        }
        InventoryVO inventoryVO = new InventoryVO();
        BeanUtils.copyProperties(inventory, inventoryVO);
        return inventoryVO;
    }

    /**
     * 根据ID获取信息
     *
     * @param id id
     * @return 实体信息
     */
    @Override
    public InventoryVO getInfoById(Long id) {
        Inventory inventory = this.getById(id);
        if (inventory == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "没有对应的信息");
        }
        return getInventoryVO(inventory);
    }

    /**
     * 保存信息
     *
     * @param inventoryAddRequest 添加请求
     * @return 新生成的ID
     */
    @Override
    @Transactional
    public Long saveInventory(InventoryAddRequest inventoryAddRequest) {
        if (inventoryAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Inventory inventory = new Inventory();
        BeanUtils.copyProperties(inventoryAddRequest, inventory);
        boolean result = this.save(inventory);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return inventory.getId();
    }


    /**
     * 修改信息
     *
     * @param inventoryUpdateRequest 修改请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean updateInventory(InventoryUpdateRequest inventoryUpdateRequest) {
        if (inventoryUpdateRequest == null || inventoryUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Inventory inventory = new Inventory();
        BeanUtils.copyProperties(inventoryUpdateRequest, inventory);
        long id = inventoryUpdateRequest.getId();
        // 判断是否存在
        Inventory oldInventory = this.getById(id);
        ThrowUtils.throwIf(oldInventory == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = this.updateById(inventory);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    /**
     * 删除信息
     *
     * @param deleteRequest 删除请求
     * @return 是否修改成功
     */
    @Override
    public Boolean deleteById(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        boolean b = this.removeById(id);
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }


    /**
     * 批量删除
     *
     * @param ids 批量删除的IDS
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public Boolean deleteBatchByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = this.removeByIds(Arrays.asList(ids));
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }
}
